package com.yuoj.backendcommon.vo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.yuoj.backendcommon.dto.question.JudgeConfig;
import com.yuoj.backendcommon.entity.Question;
import com.yuoj.backendcommon.utils.CopyUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author 刘渠好
 * @since 2025-01-12 22:39
 */
@Data
public class QuestionVO implements Serializable {

    private static final long serialVersionUID = 4681778951490268116L;
        /**
         * id
         */
        private Long id;

        /**
         * 标题
         */
        private String title;

        /**
         * 内容
         */
        private String content;

        /**
         * 标签（json数组）
         */
        private List<String> tags;


        /**
         * 题目提交数
         */
        private Integer submitNum;

        /**
         * 题目通过数
         */
        private Integer acceptNum;


        /**
         * 判题配置（json数组）
         */
        private JudgeConfig judgeConfig;

        /**
         * 点赞数
         */
        private Integer thumbNum;

        /**
         * 收藏数
         */
        private Integer favourNum;

        /**
         * 创建用户id
         */
        private UserVO userVO;

        /**
         * 创建时间
         */
        private Date createTime;

        /**
         * 删除时间
         */
        private Date updateTime;

    /**
     * 包装类转对象
     *
     * @param questionVO
     * @return
     */
    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = CopyUtil.copy (questionVO,Question.class );
        List<String> tags = questionVO.getTags ();
        if (CollUtil.isNotEmpty (tags)){
            question.setTags (JSONUtil.toJsonStr (tags));
        }
        JudgeConfig judgeConfig = questionVO.getJudgeConfig ();
        if (judgeConfig != null){
            question.setJudgeConfig (JSONUtil.toJsonStr (judgeConfig));
        }
        return question;
    }

    /**
     * 对象转包装类
     *
     * @param question
     * @return
     */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = CopyUtil.copy (question, QuestionVO.class);
        String tags = question.getTags ();
        if (StringUtils.isNotBlank (tags)){
            questionVO.setTags (JSONUtil.toList (tags, String.class));
        }
        String judgeConfig = question.getJudgeConfig ();
        if (StringUtils.isNotBlank (judgeConfig)){
            questionVO.setJudgeConfig (JSONUtil.toBean (judgeConfig, JudgeConfig.class));
        }
        return questionVO;
    }


    }
